package com.springboot.thymeleaf.springbootthymeleaf.service.impl;

import com.springboot.thymeleaf.springbootthymeleaf.entity.ThirdExpressVo;
import com.springboot.thymeleaf.springbootthymeleaf.entity.vo.CommonExpressVo;
import com.springboot.thymeleaf.springbootthymeleaf.service.ProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;

/**
 * @Classname ${小柯}
 * @Description TODO
 * @Date 2019/10/15 20:51
 * @Created by Administrator
 */

@Service
@Slf4j
public class ProcessServiceImpl implements ProcessService {


    @Override
    public CommonExpressVo process(ThirdExpressVo thirdExpressVo) throws Exception {

        long timeStart = System.currentTimeMillis();
        CommonExpressVo commonExpressVo = null;
        //第三方快递查询进度标识
        Boolean isFromThirdExpress = null;

        String companyName = thirdExpressVo.getCompanyName();//快递公司
        String expressNo = thirdExpressVo.getExpressNo();//快递单号

        try{
            String xmlText = "";

            if("query_sr".equals(companyName)){
                //调用接口，查询快递
                xmlText = queryHttpExpressDetails(companyName, expressNo);
                //解析速尔
            }

        }catch (Throwable e){

        }



        long timeEnd = System.currentTimeMillis();

        //log.error();


        return null;
    }

    /**
     * 根据快递公司，单号查询快递进度。
     * @param companyName
     * @param expressNo
     * @return
     */
    private String queryHttpExpressDetails(String companyName, String expressNo) throws Exception{

        String encode = "utf-8";
        HttpURLConnection urlConnection = null;
        BufferedReader bufferedReader = null;


        try {
            log.info("开始发送请求，根据快递" + companyName + "请求，快递单号" + expressNo);
            URL url = new URL(expressNo);
            urlConnection = (HttpURLConnection)url.openConnection();

            //设置超时、请求时间
            urlConnection.setConnectTimeout(1000*5);
            urlConnection.setReadTimeout(1000 * 5);

            bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), encode));

            String result = null;
            String b = "";
            StringBuffer stringBuffer = new StringBuffer();
            while((b = bufferedReader.readLine()) != null){
                stringBuffer.append(b);
            }

            result = stringBuffer.toString();

            log.info(">>>" + companyName + "返回结果" + result);
            return  result;



        } catch (Exception e) {
            if(e instanceof SocketTimeoutException){
                log.info("根据苏尔快递单号，请求超时");
            }else {
                log.error("根据苏尔快递单号，抛出异常" + e);
            }
            throw e;
        }finally {
            if(bufferedReader != null){
                bufferedReader.close();
            }
            if(urlConnection != null){
                urlConnection.disconnect();
            }
        }


    }
}
